package com.itboye.textileexpres.order;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.WebActivity;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class OrderWholeAdapter extends BaseAdapter implements OnClickListener {
	Context context;
	MyOrderBeans beans;
	ClickListener listOnclik;
	public static HashMap<Integer, Integer> isPraised;

	public OrderWholeAdapter(Context context, MyOrderBeans beans,
			ClickListener listOnclik) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.beans = beans;
		this.listOnclik = listOnclik;
		init();
	}

	public void init() {
		isPraised = new HashMap<Integer, Integer>();
		for (int i = 0; i < beans.getList().size(); i++) {
			isPraised.put(i, R.drawable.ic_launcher);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beans.getList().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub item_order_maijiayijingfukuan
		viewHolder holder = null;
		if (convertView == null) {
			holder = new viewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(
					R.layout.item_order_maijiayijingfukuan, null);
			holder.area_tv = (TextView) convertView.findViewById(R.id.area_tv);
			holder.state_tv = (TextView) convertView
					.findViewById(R.id.state_tv);
			holder.publish_author_tv = (TextView) convertView
					.findViewById(R.id.publish_author_tv);

			holder.childview_lin = (LinearLayout) convertView
					.findViewById(R.id.childview_lin);

			holder.state_rle = (RelativeLayout) convertView
					.findViewById(R.id.state_rle);
			holder.reyoutlcle = (RelativeLayout) convertView
					.findViewById(R.id.state_rle);

			holder.relyoutKukuan = (RelativeLayout) convertView
					.findViewById(R.id.relyoutKukuan);
			holder.btn_Confirm = (TextView) convertView
					.findViewById(R.id.btn_Confirm);
			holder.rewuliu = (RelativeLayout) convertView
					.findViewById(R.id.rewuliu);
			holder.btn_sendOk = (TextView) convertView
					.findViewById(R.id.btn_sendOk);
			holder.tvwulkiu = (TextView) convertView
					.findViewById(R.id.tvwulkiu);
			holder.cancle_tv = (TextView) convertView
					.findViewById(R.id.cancle_tv);
			holder.cancle_order = (TextView) convertView
					.findViewById(R.id.cancle_order);

			convertView.setTag(holder);
		} else {
			holder = (viewHolder) convertView.getTag();
		}
		holder.area_tv.setText("产地:" + beans.getList().get(position).getNote());
		String str = beans.getList().get(position).getQuery_status();
		if (str.equals("1")) {
			System.out.println("relyoutKukuan等待买家付款"+str);
			System.out.println("relyoutKukuan" + "fukiuabn");
			holder.state_tv.setText("等待买家付款");
			holder.reyoutlcle.setVisibility(View.GONE);
			holder.state_rle.setVisibility(View.GONE);
			holder.relyoutKukuan.setVisibility(View.VISIBLE);
			holder.rewuliu.setVisibility(View.GONE);
		} else if (str.equals("2")) {
			System.out.println("relyoutKukuan卖家已发货"+str);
			holder.state_tv.setText("卖家已发货");
			holder.rewuliu.setVisibility(View.GONE);
			holder.reyoutlcle.setVisibility(View.GONE);
			holder.state_rle.setVisibility(View.VISIBLE);
			holder.relyoutKukuan.setVisibility(View.GONE);

		} else if (str.equals("3")) {
			System.out.println("relyoutKukuan等待买家收货"+str);
			holder.state_tv.setText("等待买家收货");
			holder.rewuliu.setVisibility(View.GONE);
			holder.reyoutlcle.setVisibility(View.GONE);
			holder.state_rle.setVisibility(View.GONE);
			holder.relyoutKukuan.setVisibility(View.VISIBLE);
		} else if (str.equals("4")) {
			System.out.println("relyoutKukuan买家已付款"+str);
			holder.relyoutKukuan.setVisibility(View.GONE);
			holder.state_tv.setText("买家已付款");
			holder.rewuliu.setVisibility(View.GONE);
			holder.reyoutlcle.setVisibility(View.GONE);

		} else if (str.equals("7")) {
			System.out.println("relyoutKukuan交易成功"+str);
			holder.state_rle.setVisibility(View.GONE);
			holder.reyoutlcle.setVisibility(View.GONE);
			holder.relyoutKukuan.setVisibility(View.GONE);
			holder.rewuliu.setVisibility(View.VISIBLE);
			holder.state_tv.setText("交易成功");
		} else if (str.equals("8")) {
			System.out.println("relyoutKukuan交易关闭"+str);
			holder.rewuliu.setVisibility(View.GONE);
			holder.state_rle.setVisibility(View.GONE);
			holder.reyoutlcle.setVisibility(View.GONE);
			holder.relyoutKukuan.setVisibility(View.GONE);
			holder.state_tv.setText("交易关闭");
		}

		holder.publish_author_tv.setText("发布人:"
				+ beans.getList().get(position).getPublisher_name());

		holder.btn_Confirm.setOnClickListener(this);
		holder.cancle_order.setOnClickListener(this);
		holder.cancle_tv.setOnClickListener(this);
		holder.btn_sendOk.setOnClickListener(this);
		holder.tvwulkiu.setOnClickListener(this);
		holder.tvwulkiu.setTag(position);
		holder.cancle_order.setTag(position);
		holder.btn_Confirm.setTag(position);
		holder.btn_sendOk.setTag(position);
		holder.cancle_tv.setTag(position);

		/**
		 * 第二级
		 */

		int lenth = beans.getList().get(position).getItems().size();
		holder.childview_lin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onIntet(position);
			}
		});
		holder.childview_lin.removeAllViewsInLayout();
		int i = 0;
		for (i = 0; i < lenth; i++) {

			System.out.println(">>>>>>>>>>>>>>>>>>>" + beans.getList().size());
			View view = LayoutInflater.from(context).inflate(
					R.layout.item_order_maijiafukuan, null);
			TextView name1 = (TextView) view.findViewById(R.id.name1);
			TextView name2 = (TextView) view.findViewById(R.id.name2);
			TextView name3 = (TextView) view.findViewById(R.id.name3);
			TextView tvColor = (TextView) view.findViewById(R.id.tv_color);
			TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
			TextView num_tv = (TextView) view.findViewById(R.id.num_tv);
			TextView total_num_tv = (TextView) view
					.findViewById(R.id.total_num_tv);
			TextView total_price_tv = (TextView) view
					.findViewById(R.id.total_price_tv);
			TextView freight_tv = (TextView) view.findViewById(R.id.freight_tv);

			ImageView img = (ImageView) view.findViewById(R.id.goods_img);
			XImageLoader.load(
					GlobalConfig.IMAGE_DOUBLE
							+ beans.getList().get(position).getItems().get(i)
									.getImg(), img);
			name1.setText(beans.getList().get(position).getItems().get(i).getOrder_code());
			name2.setText(beans.getList().get(position).getItems().get(i).getName());
			name3.setText(beans.getList().get(position).getItems().get(i).getName());

			price_tv.setText(beans.getList().get(position).getItems().get(i)
					.getOri_price());
			num_tv.setText("X"
					+ beans.getList().get(position).getItems().get(i)
							.getDt_goods_unit());
			total_num_tv.setText(beans.getList().get(position).getItems()
					.get(i).getDt_origin_country());
			total_price_tv.setText("合计"+PriceChange.Change(
					 beans.getList().get(position).getItems().get(i)
							.getPrice()) + "元");
			tvColor.setText(beans.getList().get(position).getItems().get(i)
					.getSku_desc());
			freight_tv.setText("(含运费"
					+ beans.getList().get(position).getItems().get(i)
							.getWeight() + ")");
			MarginLayoutParams layoutParams = new MarginLayoutParams(
					MarginLayoutParams.WRAP_CONTENT,
					MarginLayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(5, 5, 5, 5);
	
			holder.childview_lin.addView(view, layoutParams);
		}
		return convertView;

	}

	public void onIntet(int position){
		String str = beans.getList().get(position).getQuery_status();
		if (str.equals("1")) {
			
			
			Intent intent;
			intent = new Intent(context, OrderDetailActivityFukuan.class);
			intent.putExtra("order_code", beans.getList().get(position)
					.getOrder_code());
			intent.putExtra("str", beans.getList().get(position)
					.getQuery_status());
			context.startActivity(intent);
			System.out.println("relyoutKukuan" + "fukiuabn");
		} else if (str.equals("2")) {
			Intent intent;
			intent = new Intent(context, OrderDetailActivityFahuo.class);
			intent.putExtra("order_code", beans.getList().get(position)
					.getOrder_code());
			intent.putExtra("str", beans.getList().get(position)
					.getQuery_status());
			context.startActivity(intent);

		} else if (str.equals("3")) {
			Intent intent;
			intent = new Intent(context, OrderDetailActivityShouhuo.class);
			intent.putExtra("order_code", beans.getList().get(position)
					.getOrder_code());
			intent.putExtra("str", beans.getList().get(position)
					.getQuery_status());
			context.startActivity(intent);
		} else if (str.equals("4")) {
			Intent intent;
			intent = new Intent(context, OrderDetailActivityShouhuo.class);
			intent.putExtra("order_code", beans.getList().get(position)
					.getOrder_code());
			intent.putExtra("str", beans.getList().get(position)
					.getQuery_status());
			context.startActivity(intent);

		} else if (str.equals("7")) {
			Intent intent;
			intent = new Intent(context, OrderDetailActivityJiaoyiwancheng.class);
			intent.putExtra("order_code", beans.getList().get(position)
					.getOrder_code());
			intent.putExtra("str", beans.getList().get(position)
					.getQuery_status());
			context.startActivity(intent);
		} else if (str.equals("8")) {
			Intent intent;
			intent = new Intent(context, OrderDetailActivityShibai.class);
			intent.putExtra("order_code", beans.getList().get(position)
					.getOrder_code());
			intent.putExtra("str", beans.getList().get(position)
					.getQuery_status());
			context.startActivity(intent);
		}
		
	}
	/**
	 * 查看物流
	 * 
	 * @param v
	 */
	public void onAddWuLiu(int positon, View v) {
		Intent intent = new Intent(MainApplication.context, WebActivity.class);
		intent.putExtra("url",
				"http://api.guannan.itboye.com/public/web.php/Express/index?order_code="
						+ beans.getList().get(positon).getOrder_code() + "39"
						+ IsMobileNumPhone.isPasswrd());
		intent.putExtra("title", "物流查看");
		context.startActivity(intent);
	}

	/**
	 * 确认收货
	 * 
	 * @author itboye
	 * 
	 */

	class viewHolder {
		private TextView btn_Confirm;
		private TextView area_tv;
		private TextView state_tv;
		private TextView publish_author_tv;
		private RelativeLayout relyoutKukuan, state_rle, reyoutlcle, rewuliu;
		LinearLayout childview_lin;
		private TextView btn_sendOk, tvwulkiu, cancle_tv;
		// 取消订单
		private TextView cancle_order;
		private ImageView goods_img;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_sendOk:
			listOnclik.onBtnSend(v);
			break;
		case R.id.btn_Confirm:
			listOnclik.onOkConfirm(v);
			break;
		case R.id.cancle_order:
			listOnclik.onCle(v);
			break;
		case R.id.cancle_tv:
			listOnclik.onWuliu(v);
			break;
		case R.id.tvwulkiu:
			listOnclik.onWuliu(v);
			break;

		default:
			break;
		}
	}

}
